#include "local.h"


IDBSystem *CreateDBSystem ( const char *cfg_name )
{
	return new EDBSystem(cfg_name);
}



EDBSystem::EDBSystem ( const char *cfg_name )
{
	con = CreateConsole();
	cfg = CreateConfig(con, cfg_name);

	storage = CreateStorage("database.db");
	net = CreateNetSystem(cfg->GetField("ip"), cfg->GetField("port"));

	con->Log(cfg->GetField("ip"));
	con->Log(cfg->GetField("port"));

}



EDBSystem::~EDBSystem ( void )
{
	delete storage;
	delete net;
	delete cfg;
	delete con;
}



void EDBSystem::Update ( void )
{

}



void EDBSystem::ParseQuery (INetSocket *socket, const char *query )
{
	char	*cmd = new char[10];
	int pos = 0;
	while (query[pos] && query[pos] != ' ')
	{
		pos++;
	}

	strncpy(cmd, query, pos);

	if ( strcmp(cmd, "add") == 0)
	{
		pos++;
		while (query[pos] && query[pos] != ' ')
		{
				pos++;
		}

		const char *id_str = new char[10];
		///strncpy(id_str, query + strlen(cmd) + 1, pos - strlen(cmd) - 1);

		uint id = 0;
		const char *user = "me";

		const char *result = storage->Write(id, user);
		net->Send(socket, result);

	} else if ( strcmp(cmd, "get") == 0)
	{
		uint id = 0;
		const char *result = storage->Read(id);
		net->Send(socket, result);
	}
}
